function clean(start,end) {
	event.preventDefault();
	chrome.history.deleteRange({
		'startTime': start, 
		'endTime': end
		},
		function done(){
			//TODO something awesome here
		}
	);       
}

//Search history between the dates and check if user wants to clean them
function cleanRange()
{
 //Convert the text field values to epoch seconds
 //TODO: add validation checks
 var startDt=new Date(document.getElementById('startdate').value);
 var endDt  =new Date(document.getElementById('enddate').value);
 var start= startDt.getTime();
 var end = endDt.getTime();
 
 //Search history for any text from start to end
 chrome.history.search({
       'text': '',  
       'startTime': start,  
       'endTime': end,
       'maxResults': 0
     }, 
     function(historyItems) {
        var numItems=historyItems.length;
        //Show the number of items found
        var data="Found "+numItems + " items";
        var foundText = document.createElement('b');
        foundText.appendChild(document.createTextNode(data+" "));
        foundDiv=document.getElementById('foundconfirm_div');
        while(foundDiv.firstChild){ //clear the div of content from earlier clicks
		    foundDiv.removeChild(foundDiv.firstChild);
        }
        foundDiv.appendChild(foundText);
        
        //If any items are found show the link to delete them
        if (numItems > 0){
        	var cleanRef = document.createElement('a');
			cleanRef.href = "";
			cleanRef.appendChild(document.createTextNode("Click here to clean them!"));
			cleanRef.addEventListener('click', 
										function(event){
											event.preventDefault();
                							clean(start,end);
							            },
							            false);
			foundDiv.appendChild(cleanRef);
		}//end if	
     }//end function
 )//end search
}

document.addEventListener('DOMContentLoaded', function () {  
 document.getElementById("search").addEventListener('click', clickHandler);  
}); 

function clickHandler(e) {  cleanRange(); }


